package com.mycase.im.boot.websocket.pattern.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mycase.im.boot.entity.communication.ClientMessage;
import com.mycase.im.boot.entity.communication.ResponseMessage;
import com.mycase.im.boot.websocket.WebSocketSignal;
import com.mycase.im.boot.websocket.pattern.MessageStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.Map;

/**
 * 心跳检测的策略器
 * @Author ZhuHJay
 * @Date 2021/10/12 20:58
 */
@Slf4j
@Service
public class HeartCheck implements MessageStrategy {

    @Override
    public Boolean isUse(int type) {
        return WebSocketSignal.MESSAGE_HEART_CHECK == type;
    }

    @Override
    public void executeHandler(ClientMessage clientMessage,
                               ObjectMapper objectMapper,
                               Map<String, Session> clientMaps,
                               Session mySession) throws Exception {

        log.warn("心跳检测数据包处理");
        sendMessage(objectMapper, mySession,
                    ResponseMessage.success()
                            .appendData("type", WebSocketSignal.MESSAGE_HEART_CHECK));
    }
}
